function wallRefresh() {
    if (!userIsLogged())
        return;
    $.ajax({
        url: "/Post",
        type: 'GET',
        dataType: 'json',
        data: {},
        success: function (myData) {
            $('ol.timeline').empty();
            for (i = 0; i < myData.length; i++) {
                var myListItem = $('<li id="post-' + myData[i].Id + '">');
                myListItem.addClass('bar' + myData[i].Id);
                var myPostBox = $('<div>');
                myPostBox.addClass('post_box');
                myPostBox.append('<span>' + myData[i].Content + '</span>');
                myPostBox.append('<span class="delete_button"><a href="#" id="' + myData[i].Id + '" class="delete_update">X</a></span>');
                myPostBox.append('<span class="feed_link"><a href="#" class="comment" id="' + myData[i].Id + '">comment</a></span>');
                myListItem.append(myPostBox);
                var myFullBox = $('<div>');
                myFullBox.addClass('fullbox' + myData[i].Id);
                myFullBox.append('<div id="commentload' + myData[i].Id + '">');
                var myCommentBox = $('<div class="comment_box" id="c' + myData[i].Id + '">');
                var myCommentForm = $('<form method="post" action="" name="' + myData[i].Id + '">');
                myCommentForm.append('<textarea class="text_area" name="comment_value" id="textarea' + myData[i].Id + '"></textarea><br />');
                myCommentForm.append('<input type="submit" value=" Partager " class="comment_submit" id="' + myData[i].Id + '"/>');
                myCommentBox.append(myCommentForm);
                myFullBox.append(myCommentBox);
                myListItem.append(myFullBox);
                $("ol#update").prepend(myListItem);
                $("ol#update li:first").slideDown("slow");
                document.getElementById('update_box_content').value = '';
                $('#update_box_content').value = '';
                $('#update_box_content').focus();
                $("#flash").hide();

                //comment slide
                $('li#post-' + myData[i].Id + ' a.comment').click(function (e) {
                    e.preventDefault();
                    var ID = $(this).attr("id");
                    $(".fullbox" + ID).show();
                    $("#c" + ID).slideToggle(300);
                    return false;
                });

                // Suppression d'un post.
                $('li#post-' + myData[i].Id + ' a.delete_update').click(function (e) {
                    e.preventDefault();
                    var ID = $(this).attr("id");
                    if (confirm("Êtes-vous sûr? Pas de Control-Z ici!"))
                        $.ajax({
                            url: "/Post/Delete",
                            type: 'DELETE',
                            cache: false,
                            dataType: 'json',
                            data: {
                                'Id': ID
                            },
                            success: function (myData) {
                                $(".bar" + ID).slideUp();
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                alert("Communication avec le serveur impossible. Merci de réessayer ultérieurement");
                            }
                        });
                    return false;
                });
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert("Communication avec le serveur impossible. Merci de réessayer ultérieurement");
        }
    });
}


$(document).ready(function () {
    // Interception de l'envoi du formulaire.
    $("#wallForm").submit(function (e) {
        e.preventDefault();
        return false;
    });


    // Envoi d'un nouveau post.
    $(".comment_button").click(function () {
        var element = $(this);
        var myUserId = userGetSessionDetails().UserId;
        var boxval = $("#update_box_content").val();

        if (boxval == '')
            alert("Merci d'entrer quelque chose à partager..");
        else {
            $("#flash").show();
            $("#flash").fadeIn(400).html('<img src="/Content/images/ajax.gif" align="absmiddle">&nbsp;<span class="loading">Loading Update...</span>');
            $.ajax({
                type: "POST",
                url: "/Post/Create",
                dataType: 'json',
                data: {
                    'UserId': userGetSessionDetails().UserId,
                    'Content': boxval
                },
                cache: false,
                success: function (myData) {
                    var myCurrentTimestamp = Math.round((new Date()).getTime() / 1000);
                    var myListItem = $('<li>');
                    myListItem.addClass('bar' + myCurrentTimestamp);
                    var myPostBox = $('<div>');
                    myPostBox.addClass('post_box');
                    myPostBox.append('<span>' + boxval + '</span>');
                    myPostBox.append('<span class="delete_button"><a href="#" id="delete_update' + myCurrentTimestamp + '" class="delete_update">X</a></span>');
                    myPostBox.append('<span class="feed_link"><a href="#" class="comment" id="comment' + myCurrentTimestamp + '">comment</a></span>');
                    myListItem.append(myPostBox);
                    var myFullBox = $('<div>');
                    myFullBox.addClass('fullbox' + myCurrentTimestamp);
                    myFullBox.append('<div id="commentload' + myCurrentTimestamp + '">');
                    var myCommentBox = $('<div class="comment_box" id="c' + myCurrentTimestamp + '">');
                    var myCommentForm = $('<form method="post" action="" name="' + myCurrentTimestamp + '">');
                    myCommentForm.append('<textarea class="text_area" name="comment_value" id="textarea' + myCurrentTimestamp + '"></textarea><br />');
                    myCommentForm.append('<input type="submit" value=" Partager " class="comment_submit" id="' + myCurrentTimestamp + '"/>');
                    myCommentBox.append(myCommentForm);
                    myFullBox.append(myCommentBox);
                    myListItem.append(myFullBox);
                    $("ol#update").prepend(myListItem);
                    $("ol#update li:first").slideDown("slow");
                    document.getElementById('update_box_content').value = '';
                    $('#update_box_content').value = '';
                    $('#update_box_content').focus();
                    $("#flash").hide();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("Communication avec le serveur impossible. Merci de réessayer ultérieurement");
                }
            });
        }
        return false;
    });


    // Envoi d'un commentaire.
    $('.comment_submit').live("click", function (e) {
        e.preventDefault();
        var ID = $(this).attr("id");
        var comment_content = $("#textarea" + ID).val();
        var dataString = 'comment_content=' + comment_content;

        if (comment_content == '')
            alert("Merci d'entrer du texte dans votre commentaire.");
        else {
            /*$.ajax({
            type: "POST",
            url: "/PostComment/Create",
            data: dataString,
            cache: false,
            success: function(html){
            $("#commentload"+ID).append(html);
            document.getElementById("textarea"+ID).value='';
            $("#textarea"+ID).focus();
            }
            });*/
            var myCurrentTimestamp = Math.round((new Date()).getTime() / 1000);
            var myCommentContainer = $('<div class=comment_load id="comment' + myCurrentTimestamp + '">');
            myCommentContainer.text(comment_content);
            myCommentContainer.append('<span class="cdelete_button"><a href="#" id="' + myCurrentTimestamp + '" class="cdelete_update">X</a></span>');
            $("#commentload" + ID).append(myCommentContainer);
            document.getElementById("textarea" + ID).value = '';
            $("#textarea" + ID).focus();
        }

        return false;
    });

    // Suppression d'un commentaire
    $('.cdelete_update').live("click", function (e) {
        e.preventDefault();
        var ID = $(this).attr("id");
        if (confirm("Êtes-vous sûr? Pas de Control-Z ici!"))
            $("#comment" + ID).slideUp();
        return false;
    });
});